
package com.example.filetransfer.publicsource.fileutils;

import com.example.filetransfer.publicsource.Log;
import com.example.filetransfer.publicsource.connection.Protocol;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DirUtils {
    private static final String TAG = "DirUtils";

    public static Dir scanDir(Dir parent, File file) {
        Dir rootDir = null;
        if (!file.isDirectory()) {
            if (parent != null) {
                parent.addChildFile(new MyFile(file.getAbsolutePath(), file.lastModified() + ""));
            }
        } else {
            File childs[] = file.listFiles();
            if (childs == null)
                return null;
            Dir current = new Dir(file.getAbsolutePath());
            for (File child : childs) {
                scanDir(current, child);
            }
            if (parent == null) {
                rootDir = current;
            } else {
                parent.addChildDir(current);
            }
        }
        return rootDir;
    }

    public static void checkDirExist(String path) {
        File dirFile = new File(path);
        if (dirFile.exists() && dirFile.isFile()) {
            dirFile.delete();
        }
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
    }

    public static List<MyFile> listDirFiles(Dir source) {
        List<MyFile> out = new ArrayList<MyFile>();
        List<Dir> dirList = source.getChildDirs();
        for (Dir dir : dirList) {
            out.addAll(listDirFiles(dir));
        }
        List<MyFile> fielList = source.getChildFiles();
        out.addAll(fielList);
        return out;
    }

    public static List<MyFile> listDirFiles(String dirPath) {
        Dir dir = scanDir(null, new File(dirPath));
        if (dir != null) {
            return listDirFiles(dir);
        }
        return new ArrayList<MyFile>();
    }

    // public static long allFileSize(List<MyFile> files) {
    // long size = 0;
    // for (MyFile file : files) {
    // size += file.getFile().length();
    // }
    // return size;
    // }

    public static String createFile(String path, String name) {
        if (path == null || path.isEmpty()) {
            return name;
        } else if (name == null || name.isEmpty()) {
            return path;
        } else {
            return path + File.separator + name;
        }
    }

    public static void main(String[] args) {
        System.out.println(createFile(null, "test.xml"));
    }

    public static String parseSeparator(String path) {
        String currentSeparator = File.separator;
        path = path.replace("/", currentSeparator);
        path = path.replace("\\", currentSeparator);
        return path;
    }

    public static String getFileName(String path) {
        int indexOfRight = path.lastIndexOf("\\");
        int indexOfLeft = path.lastIndexOf("/");
        if (indexOfRight != -1 && indexOfLeft != -1) {
            return path.substring(Math.min(indexOfRight, indexOfLeft) + 1);
        } else if (indexOfRight != -1) {
            return path.substring(indexOfRight + 1);
        } else if (indexOfLeft != -1) {
            return path.substring(indexOfLeft + 1);
        } else {
            return path;
        }
    }

    public static String getFilePath(String url) {
        url = url.replace("\\", "/");
        int index = url.lastIndexOf("/");
        if (index != -1) {
            return url.substring(0, index);
        }
        return "";
    }

    public static void writeNewTree(String path) {
        Log.d(TAG, "write new file tree");
        XmlSaver saver = new XmlSaver(DirUtils.createFile(path, Protocol.FILETREENAME));
        File file = new File(path);
        Dir rootDir = DirUtils.scanDir(null, file);
        saver.saveDir(rootDir);
    }
}
